import {
  registerSubmit,
  getCode
} from '../../../utils/networkRequest.js'
import {
  errorMsg
} from '../../../utils/tool.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: "",
    phone: "",
    vercode: "",
    downtime:60
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let {
      i,
      t
    } = options;
    this.setData({
      i: i,
      t: t
    })
  },
  //获取姓名
  getName: function(e) {
    this.setData({
      name: e.detail.value
    })

  },
  //获取手机号
  getPhone: function(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  //获取验证码
  getVerCode: function(e) {
    this.setData({
      vercode: e.detail.value
    })
  },
  //发送验证码
  getCode: function () {
    let self = this;
    let { phone } = this.data;
    if (!/^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/.test(phone)) {
      errorMsg('手机号不正确');
    } else {
      if (this.data.downtime == 60) {
        getCode(phone,"newRegister", (r) => {

          if (r.code == 0) {
            this.setData({
              downtime: --self.data.downtime
            })
            let siv = setInterval(() => {
              if (self.data.downtime > 1) {
                self.setData({
                  downtime: --self.data.downtime
                })
              } else {
                clearInterval(siv);
                self.setData({
                  downtime: 60
                })
              }

            }, 1000)
          } else {
            errorMsg(r.message);
          }
        })
      }
    }

  },
  //提交注册信息
  onSubmit: function() {
    let {
      name,
      phone,
      vercode,
      i,
      t
    } = this.data;
    if (name == "") {
      errorMsg("请输入姓名");
    } else if (!/^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/.test(phone)) {
      errorMsg("手机号码不正确");
    } else if (vercode == "") {
      errorMsg("请输入验证码");
    } else {
      registerSubmit(name, phone, vercode, i, t,"newRegister", (r) => {
        if (r.code == 0) {
          wx.reLaunch({
            url: '../../index/index',
          })
          setTimeout(function(){
            errorMsg(r.message);
          },400)
         
        } else {
          errorMsg(r.message)
        }
      })
    } 
  }
})